﻿using CMS.BaseEntity;
using CMS.BusinessLogic;
using CMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMS.UserControl
{
    public partial class EditProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSectionList();
                BindCategoryList();
                BindBonusList();
                if (Request.QueryString["pid"] != null)
                {
                    BindDetailToEdit();
                }
            }
        }

        ProductLogic pLogic = new ProductLogic();
        CategoryLogic cLogic = new CategoryLogic();
        SectionLogic sLogic = new SectionLogic();
        protected void BindDetailToEdit()
        {
            Product product = new Product();
            product.ProductId = int.Parse(Request.QueryString["pid"].ToString());
            DataTable dt = pLogic.GetById(product);
            if (dt.Rows.Count != 1)
            {
                Response.Redirect(Directory.Error_Page);
            }
            else
            {
                DataRow dr = dt.Rows[0];
                txtName.Text = dr["Name"].ToString();
                ddlSection.SelectedValue = dr["SectionId"].ToString();
                ddlCategory.SelectedValue = dr["CategoryId"].ToString();
                txtDescription.Text = dr["Description"].ToString();
                imgProduct.ImageUrl = dr["Image"].ToString();
                txtIntro.Text = dr["Intro"].ToString();
                txtQuantity.Text = dr["Quantity"].ToString();
                txtManufactory.Text = dr["Manufacture"].ToString();
                ddlGuarantee.SelectedValue = dr["Guarantee"].ToString();
                ddlBonus.SelectedValue = dr["BonusId"].ToString();
                txtDate.Text = DateTime.Now.ToString();
                txtCurrentPrice.Text = dr["CurrentPrice"].ToString();
                txtOldPrice.Text = dr["OldPrice"].ToString();
            }
        }

        protected void BindSectionList()
        {
            DataTable dt = sLogic.GetListSection();
            ddlSection.DataSource = dt;
            ddlSection.DataTextField = "SectionName";
            ddlSection.DataValueField = "SectionId";
            ddlSection.DataBind();
        }

        protected void BindCategoryList()
        {
            DataTable dt = cLogic.GetListCategory();
            ddlCategory.DataSource = dt;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataBind();
        }

        protected void BindBonusList()
        {
            DataTable dt = pLogic.GetListBonus();
            ddlBonus.DataSource = dt;
            ddlBonus.DataValueField = "BonusId";
            ddlBonus.DataTextField = "BonusName";
            ddlBonus.DataBind();
        }

        protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Product product = new Product();
            product.ProductId = int.Parse(Request.QueryString["pid"].ToString());
            product.SectionId = int.Parse(ddlSection.SelectedValue.ToString());
            product.CategoryId = int.Parse(ddlCategory.SelectedValue.ToString());
            product.Quantity = int.Parse(txtQuantity.Text);
            product.Name = txtName.Text;
            product.Description = txtDescription.Text;
            product.Intro = txtIntro.Text;
            product.Manufacture = txtManufactory.Text;
            product.Guarantee = int.Parse(ddlGuarantee.SelectedValue.ToString());
            product.BonusId = int.Parse(ddlBonus.SelectedValue.ToString());
            product.AuthorId = int.Parse(Session["UserId"].ToString());
            product.CurrentPrice = double.Parse(txtCurrentPrice.Text);
            if (txtOldPrice.Text != "")
            {
                product.OldPrice = double.Parse(txtOldPrice.Text);
            }

            product.LastModify = DateTime.Now.ToString();
            if (chkEnable.Checked == true)
                product.Enable = true;
            else
                product.Enable = false;
            if (chkFocus.Checked == true)
                product.Focus = true;
            else
                product.Focus = false;

            if (fuImage.HasFile)
            {
                string fileName = Directory.RootPath_Image + DateTime.Now.ToString("ddMMyyyy_hhmmss_tt_") + fuImage.FileName;
                string filePath = MapPath(fileName);
                fuImage.SaveAs(filePath);
                product.Image = fileName;
            }
            else
                product.Image = imgProduct.ImageUrl.ToString();

            if (pLogic.IsUpdated(product))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }
    }
}